package com.xxcxcxcx.xc_moremob.common.skills.SkillEvokerAttack;

import com.xxcxcxcx.xc_moremob.common.entities.monster.realization.SkillBasicEntity;
import com.xxcxcxcx.xc_moremob.common.skills.Currentskills;
import com.xxcxcxcx.xc_moremob.common.skills.UseskillGoal;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.EvokerFangs;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;

//地刺攻击
public class EvokerAttackSkillGoal extends UseskillGoal {
    public EvokerAttackSkillGoal(SkillBasicEntity mo) {
        super(mo,20,40,100);
    }
    public EvokerAttackSkillGoal(SkillBasicEntity mo, int CastWarmup, int Casting, int CastingInterval){
        super(mo,CastWarmup,Casting,CastingInterval);
    }
    protected void performSpellCasting() {
        LivingEntity livingentity = mob.getTarget();
        double d0 = Math.min(livingentity.getY(), mob.getY());
        double d1 = Math.max(livingentity.getY(), mob.getY()) + 1.0D;
        float f = (float) Mth.atan2(livingentity.getZ() - mob.getZ(), livingentity.getX() - mob.getX());
        if (mob.distanceToSqr(livingentity) < 9.0D) {
            for(int i = 0; i < 5; ++i) {
                float f1 = f + (float)i * (float)Math.PI * 0.4F;
                this.createSpellEntity(mob.getX() + (double)Mth.cos(f1) * 1.5D, mob.getZ() + (double)Mth.sin(f1) * 1.5D, d0, d1, f1, 0);
            }

            for(int k = 0; k < 8; ++k) {
                float f2 = f + (float)k * (float)Math.PI * 2.0F / 8.0F + 1.2566371F;
                this.createSpellEntity(mob.getX() + (double)Mth.cos(f2) * 2.5D, mob.getZ() + (double)Mth.sin(f2) * 2.5D, d0, d1, f2, 3);
            }
        } else {
            for(int l = 0; l < 16; ++l) {
                double d2 = 1.25D * (double)(l + 1);
                int j = 1 * l;
                this.createSpellEntity(mob.getX() + (double)Mth.cos(f) * d2, mob.getZ() + (double)Mth.sin(f) * d2, d0, d1, f, j);
            }
        }

    }

    private void createSpellEntity(double p_32673_, double p_32674_, double p_32675_, double p_32676_, float p_32677_, int p_32678_) {
        BlockPos blockpos = new BlockPos(p_32673_, p_32676_, p_32674_);
        boolean flag = false;
        double d0 = 0.0D;

        do {
            BlockPos blockpos1 = blockpos.below();
            BlockState blockstate = mob.level.getBlockState(blockpos1);
            if (blockstate.isFaceSturdy(mob.level, blockpos1, Direction.UP)) {
                if (!mob.level.isEmptyBlock(blockpos)) {
                    BlockState blockstate1 = mob.level.getBlockState(blockpos);
                    VoxelShape voxelshape = blockstate1.getCollisionShape(mob.level, blockpos);
                    if (!voxelshape.isEmpty()) {
                        d0 = voxelshape.max(Direction.Axis.Y);
                    }
                }
                flag = true;
                break;
            }
            blockpos = blockpos.below();
        } while(blockpos.getY() >= Mth.floor(p_32675_) - 1);

        if (flag) {
            mob.level.addFreshEntity(new EvokerFangs(mob.level, p_32673_, (double)blockpos.getY() + d0, p_32674_, p_32677_, p_32678_, mob));
        }

    }

    protected SoundEvent getSpellPrepareSound() {
        return SoundEvents.EVOKER_PREPARE_ATTACK;
    }

    protected Currentskills getSpell() {
        return Currentskills.FANGS;
    }
}
